import { Controller } from "../../mvc/core/Controller";
import { SecondCareerSelectWin } from "../view/secondCareer/SecondCareerSelectWin";
import { SecondCareerTransformWin } from "../view/secondCareer/SecondCareerTransformWin";
import { SecondCareerGetTaskWin } from "../view/secondCareer/SecondCareerGetTaskWin";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DialogGuideNextShowingOpData } from "../view/dialog/data/DialogGuideNextShowingOpData";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { DataEvent } from "../events/DataEvent";
import { AIManager } from "../scene/ai/AIManager";
import { AutoFight } from "../view/autoFight/AutoFight";
import { CareerUtil } from "../view/common/util/CareerUtil";
import { SystemSetter } from "../view/system/SystemSetter";
import { SecondCareerTaskFinishStatus } from "../view/secondCareer/SecondCareerTaskFinishStatus";
import { SecondCareerTaskIcon } from "../view/secondCareer/SecondCareerTaskIcon";
import { Language } from "../../../extend/language/Language";
import { ConstConfig } from "../resource/ConstConfig";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { SFightAttribute } from "../../../Message/Public/SFightAttribute";
import { SecondCrareerAttribute } from "../view/secondCareer/selectSubWin/SecondCrareerAttribute";
import { SecondCareerUtil } from "../view/secondCareer/util/SecondCareerUtil";
import { GameConst } from "../../component/gconst/GameConst";
import { SecondCareerDescData } from "../view/secondCareer/items/SecondCareerDescData";
import { ClockManager } from "../manager/ClockManager";
type int = number;
//class SecondCareerController
    
    export  class SecondCareerController extends Controller
    {
       

        /* internal  */get selectWin(): SecondCareerSelectWin
        {
            if (this._selectWin == null) 
            {
                this._selectWin = new SecondCareerSelectWin();
                this._selectWin.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onSelectWinAddToStage);
            }
            return this._selectWin;
        }

        /* internal  */get resetWin(): SecondCareerSelectWin
        {
            if (this._resetWin == null) 
            {
                this._resetWin = new SecondCareerSelectWin();
                this._resetWin.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.changeSecondCareerAddToStageHandler);
            }
            return this._resetWin;
        }

        /* internal  */get transformWin(): SecondCareerTransformWin
        {
            if (this._transformWin == null) 
            {
                this._transformWin = new SecondCareerTransformWin();
                this._transformWin.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onTransformWinAddToStage);
            }
            return this._transformWin;
        }

        public get getTaskWin(): SecondCareerGetTaskWin
        {
            if (this._getTaskWin == null) 
            {
                this._getTaskWin = new SecondCareerGetTaskWin();
                this._getTaskWin.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onGetTaskWinAddToStage);
            }
            return this._getTaskWin;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerSecondUpdate, this.secondCareerSelectedHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerEnableUpdate, this.currentCareerChangedHandler);
            Dispatcher.addEventListener(EventName.SecondCareerOpenTaskWin, this.openTaskWinHandler);
            Dispatcher.addEventListener(EventName.SecondCareerOpenSelectWin, this.openSelectWinHandler);
            Dispatcher.addEventListener(EventName.SecondCareerOpenChangeSecondCareer, this.openChangeSecondCareerHandler);
            Dispatcher.addEventListener(EventName.SecondCareerOpenTransformWin, this.openTtransformWinHandler);
            Dispatcher.addEventListener(EventName.SecondCareerSelect, this.selectSecondCareerHandler);
            Dispatcher.addEventListener(EventName.SecondCareerTransfrom, this.transformHandler);
            Dispatcher.addEventListener(EventName.SecondCareerItemSelected, this.secondCareerItemSelectedHandler);
            Dispatcher.addEventListener(EventName.SecondCareerShowGetTaskPanel, this.showGetTaskPanelHandler);
            Dispatcher.addEventListener(EventName.SecondCareerIGetTask, this.getSecondCareerTaskHandler);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.showHintIconHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.showHintIconHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerUpdate, this.showHintIconHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetResHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            Dispatcher.addEventListener(EventName.SecondCareerTransformCdGetSuccess, this.updateTransformCd);
            Dispatcher.addEventListener(EventName.SecondCareerGetOthersAttrSuccess, this.onGetOtherCareerAttrHandler);
            Dispatcher.addEventListener(EventName.AI_NpcOutDestance, this.outNpcDistanceHandler);
            return;
        }

        /* internal  */secondCareerSelectedHandler(arg1: Object): void
        {
            var loc3=null;
            if (this._hintIcon != null) 
            {
                this._hintIcon.hide();
                this._hintIcon = null;
            }
            var loc1=!(this._curWin == null) && this._curWin == this._selectWin;
            if (!(this._selectWin == null) && !this._selectWin.isHide) 
            {
                this._selectWin.hide();
                this._selectWin = null;
            }
            if (!(this._resetWin == null) && !this._resetWin.isHide) 
            {
                this._resetWin.hide();
                this._resetWin = null;
            }
            this._curWin = null;
            if (loc1) 
            {
                (loc3 = new DialogGuideNextShowingOpData()).isNextShowRelease = false;
                loc3.showArrow = true;
                loc3.showLineBox = true;
                loc3.operateId = ENpcEffect._ENpcEffectSecondCareerTransform;
                Dispatcher.dispatchEvent(new DataEvent(EventName.NpcGuidNextShwoingOpList, loc3));
            }
            var loc2=cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectSecondCareerTransform);
            if (loc2 != null) 
            {
                AIManager.onAutoPathAIControl(loc2);
            }
            AutoFight.localAutoFight.clearToBaseAndSecond();
            Dispatcher.dispatchEvent(new DataEvent(EventName.SaveAutoFightInfo));
            return;
        }

        /* internal  */currentCareerChangedHandler(arg1: Object): void
        {
            if (CareerUtil.isGradeCareer(cache.role.careerEnabled)) 
            {
                this.updateTransformWin();
            }
            return;
        }

        /* internal  */showHintIconHandler(arg1=null): void
        {
            if (!cache.secondCareer.isCareerLevelFit()) 
            {
                return;
            }
            this.showHintIcon();
            return;
        }

        /* internal  */onTaskGetResHandler(arg1: Object): void
        {
            this.showHintIconHandler();
            if (!cache.secondCareer.isTaskCodeAndCareerFit(arg1, cache.secondCareer.getGetTaskCode(cache.role.careerBasic))) 
            {
                return;
            }
            SystemSetter.localSetter.secondCareerTaskFinishStatus = SecondCareerTaskFinishStatus.Doing;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow));
            return;
        }

        /* internal  */onTaskEndRes(arg1: Object): void
        {
            this.showHintIconHandler();
            if (!cache.secondCareer.isTaskCodeAndCareerFit(arg1, cache.secondCareer.getEndTaskCode())) 
            {
                return;
            }
            SystemSetter.localSetter.secondCareerTaskFinishStatus = SecondCareerTaskFinishStatus.FinishTask;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow));
            this.openSelectWinHandler(null);
            return;
        }

        /* internal  */hideHitIcon(): void
        {
            if (this._hintIcon != null) 
            {
                this._hintIcon.hide();
            }
            return;
        }

        /* internal  */showHintIcon(): void
        {
            if (this._hintIcon == null) 
            {
                this._hintIcon = new SecondCareerTaskIcon();
            }
            this._hintIcon.show();
            return;
        }

        /* internal  */openTaskWinHandler(arg1: DataEvent): void
        {
            this.getTaskWin.show();
            return;
        }

        /* internal  */openSelectWinHandler(arg1: DataEvent): void
        {
            this.selectWin.show();
            return;
        }

        /* internal  */openChangeSecondCareerHandler(arg1: DataEvent): void
        {
            this.resetWin.show();
            return;
        }

        /* internal  */changeSecondCareerAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc2=0;
            this._curWin = this.resetWin;
            var loc1=CareerUtil.getOtherGradeCareer(cache.role.careerBasic, cache.role.careerSecond);
            this.resetWin.updateSelectWin(loc1);
            loc2 = this.selectWin.getCurSelectedCareer();
            this.selectWinChangeToCareer(loc2);
            var loc3=cache.role.money.gold;
            var loc4=888;
            var loc5=loc3 >= loc4;
            this.resetWin.updateNeedCoins(loc4, loc5, true);
            this.resetWin.descTips = Language.getString(80611);
            this.resetWin.title = Language.getString(80610);
            this.updateRuneWin(loc2);
            return;
        }

        /* internal  */onSelectWinAddToStage(arg1: Object /* flash.events.Event */): void
        {
            this._curWin = this.selectWin;
            var loc1=CareerUtil.getOthersGradeCareer(cache.role.careerBasic);
            this.selectWin.updateSelectWin(loc1);
            var loc2=this.selectWin.getCurSelectedCareer();
            this.selectWinChangeToCareer(loc2);
            var loc3=cache.role.money.coin + cache.role.money.coinBind;
            var loc4=ConstConfig.instance.getValueByName("CareerSecondOpenCost");
            var loc5=loc3 >= loc4;
            this.selectWin.updateNeedCoins(loc4, loc5);
            this.updateRuneWin(loc2);
            return;
        }

        /* internal  */openTtransformWinHandler(arg1: DataEvent): void
        {
            this.transformWin.show();
            return;
        }

        /* internal  */transformHandler(arg1: DataEvent): void
        {
            var loc1=ConstConfig.instance.getValueByName("CareerSecondSwitchConst");
            if (this.checkAndShowBuyCoin(loc1)) 
            {
                return;
            }
            GameProxy.roleProxy.secondCrareerTransform();
            return;
        }

        /* internal  */checkAndShowBuyCoin(arg1: int): Boolean
        {
            var loc1=cache.login.loginGame.money.coin + cache.login.loginGame.money.coinBind;
            if (arg1 > loc1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(20681));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, arg1));
                return true;
            }
            return false;
        }

        /* internal  */secondCareerItemSelectedHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            this.selectWinChangeToCareer(loc1);
            return;
        }

        /* internal  */showGetTaskPanelHandler(arg1: DataEvent): void
        {
            this.getTaskWin.show();
            return;
        }

        /* internal  */getSecondCareerTaskHandler(arg1: DataEvent): void
        {
            GameProxy.taskProxy.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, cache.secondCareer.getGetTaskCode(cache.role.careerBasic));
            return;
        }

        /* internal  */selectSecondCareerHandler(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=0;
            if (this._curWin != this._selectWin) 
            {
                loc2 = cache.login.loginGame.money.gold;
                if (888 > loc2) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(41005));
                    return;
                }
                GameProxy.roleProxy.reselectSecondCareer(int(arg1.data));
            }
            else 
            {
                loc1 = ConstConfig.instance.getValueByName("CareerSecondOpenCost");
                if (this.checkAndShowBuyCoin(loc1)) 
                {
                    return;
                }
                GameProxy.roleProxy.openSecondCareer(int(arg1.data));
            }
            return;
        }

        /* internal  */updateRuneWin(arg1: int): void
        {
            if (arg1 <= 0) 
            {
                arg1 = cache.role.careerBasic;
            }
            var loc1=cache.pack.runePackCache.getAllItemsByCareer(arg1);
            var loc2=[];
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                if (loc1[loc3] != null) 
                {
                    loc2.push(loc1[loc3]);
                }
                ++loc3;
            }
            this._curWin.updateSkillRuneWin(loc2);
            return;
        }

        /* internal  */updateRoleInfoWin(arg1: int): void
        {
            var loc1=cache.role.isShowWing > 0;
            var loc2=cache.role.isShowFashion > 0;
            var loc3=cache.role.isShowFashionWeapon > 0;
            this._curWin.updateRoleEquip(cache.role.playerInfo.name, cache.role.roleInfo.level, arg1, cache.pack.rolePackCache.getAllItemsByCareer(arg1), cache.role.entityInfo.camp, cache.role.entityInfo.sex, cache.role.entityInfo.wuxingType, loc2, loc3, loc1);
            return;
        }

        /* internal  */selectWinChangeToCareer(arg1: int): void
        {
            if (arg1 <= 0) 
            {
                arg1 = cache.role.careerBasic;
            }
            this._tempCareer = arg1;
            GameProxy.roleProxy.getOtherCareerFightAttr(arg1);
            this.updateRuneWin(arg1);
            this.updateRoleInfoWin(arg1);
            return;
        }

        /* internal  */onGetOtherCareerAttrHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SFightAttribute;
            if (this._tempCareer <= 0 || this._curWin == null) 
            {
                return;
            }
            var loc2=SecondCrareerAttribute.AdditionsP;
            if (CareerUtil.isMagicCareer(this._tempCareer)) 
            {
                loc2 = SecondCrareerAttribute.AdditionsM;
            }
            var loc3;
            (loc3 = SecondCareerUtil.getSecondAttrsByAdictions(loc1, loc2, this._tempCareer, this._tempCareer)).push(cache.role.combatCapabilities);
            this._curWin.updateAttrWin(loc3);
            return;
        }

        /* internal  */outNpcDistanceHandler(arg1: DataEvent): void
        {
            if (!(this._transformWin == null) && !this._transformWin.isHide) 
            {
                this._transformWin.hide();
                this._transformWin.dispose(false);
                this._transformWin = null;
            }
            return;
        }

        /* internal  */onTransformWinAddToStage(arg1: Object /* flash.events.Event */): void
        {
            this.updateTransformWin();
            return;
        }

        /* internal  */onGetTaskWinAddToStage(arg1: Object /* flash.events.Event */): void
        {
            var loc6=0;
            var loc1=cache.role.roleInfo.level >= GameConst.SecondCareerLevel ? true : false;
            var loc2=CareerUtil.isGradeCareer(cache.role.roleInfo.career);
            var loc3=CareerUtil.getOthersGradeCareer(cache.role.roleInfo.career);
            var loc4=[];
            var loc5=0;
            while (loc5 < loc3.length) 
            {
                loc6 = int(loc3[loc5]);
                loc4.push(new SecondCareerDescData(loc6, CareerUtil.getGradeCareerDesc(loc6)));
                ++loc5;
            }
            this.getTaskWin.updateView(loc4, loc1, loc2);
            return;
        }

        /* internal  */updateTransformWin(arg1=null): void
        {
            var loc1=cache.role.money.coin + cache.role.money.coinBind;
            var loc2=ConstConfig.instance.getValueByName("CareerSecondSwitchConst");
            var loc3=loc1 >= loc2;
            this.transformWin.updateView(cache.role.careerBasic, cache.role.careerSecond, cache.role.careerEnabled, loc2, loc3);
            GameProxy.roleProxy.getSecondCarreerTransfromCd();
            return;
        }

        /* internal  */updateTransformCd(arg1: DataEvent): void
        {
            var loc1=arg1.data as Date;
            var loc2=ClockManager.instance.nowDate;
            var loc3=0;
            if (loc1.time > loc2.time) 
            {
                loc3 = (loc1.time - loc2.time) / 1000;
            }
            if (this._transformWin && !this._transformWin.isHide) 
            {
                this._transformWin.updateCdTime(loc3);
            }
            return;
        }

        private /* var */_hintIcon: SecondCareerTaskIcon;

        private /* var */_tempCareer: int=-1;

        private /* var */_resetWin: SecondCareerSelectWin;

        private /* var */_curWin: SecondCareerSelectWin;

        private /* var */_selectWin: SecondCareerSelectWin;

        private /* var */_getTaskWin: SecondCareerGetTaskWin;

        private /* var */_transformWin: SecondCareerTransformWin;
    }
